package com.sihai.ehr.web.action.train;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.AuditType;
import com.sihai.ehr.model.biz.Duty;
import com.sihai.ehr.model.biz.EmployeeinfoWork;
import com.sihai.ehr.model.biz.TrainCategory;
import com.sihai.ehr.model.biz.TrainClassroom;
import com.sihai.ehr.model.biz.TrainLecturer;
import com.sihai.ehr.model.biz.TrainMaterResource;
import com.sihai.ehr.model.biz.TrainPlan;
import com.sihai.ehr.model.biz.TrainPlanAuditing;
import com.sihai.ehr.model.biz.TrainPlanDutyTarget;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.DutyService;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.service.TrainCategoryService;
import com.sihai.ehr.service.TrainClassroomService;
import com.sihai.ehr.service.TrainLecturerService;
import com.sihai.ehr.service.TrainMaterResourceService;
import com.sihai.ehr.service.TrainPlanDutyTargetService;
import com.sihai.ehr.service.TrainPlanService;
import com.sihai.ehr.web.action.EHRAction;
import com.sihai.ehr.web.pojo.TrainPlanDutyTargetPojo;
import com.sihai.ehr.web.pojo.TrainPlanEmployeeTargetPojo;

/**
 * 培训计划控制器
 * 
 * @author henry
 * 
 */
@Results({
		@Result(name = "main", location = "/WEB-INF/pages/train/train_main.jsp"),
		@Result(name = "plan_manage", location = "plan_manage.jsp"),
		@Result(name = "list", location = "plan_list.jsp"),
		@Result(name = "target_sel_main", location = "plan_target_sel_main.jsp"),
		@Result(name = "target_sel_list", location = "plan_target_sel_list.jsp"),
		@Result(name = "plan_list_audit", location = "plan_list_audit.jsp"),
		@Result(name = "plan_audit", location = "plan_audit.jsp"),
		@Result(name = "plan_list_browse", location = "plan_list_browse.jsp"),
		@Result(name = "plan_list_execute", location = "plan_list_execute.jsp"),
		@Result(name = "plan_signin", location = "plan_signin.jsp"),
		@Result(name = "plan_notify", location = "plan_notify.jsp"),
		@Result(name = "plan_person_list",location = "/WEB-INF/pages/train/plan_person_list.jsp"),
		@Result(name = "staff_plan_list",location = "/WEB-INF/pages/train/staff_plan_list.jsp")})
public class TrainPlanAction extends EHRAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(TrainPlanAction.class);

	@BeanInject
	private OrganizationService organizationService;
	@BeanInject
	private DutyService dutyService;
	@BeanInject
	private TrainPlanService trainPlanService;
	@BeanInject
	private TrainCategoryService trainCategoryService; // 培训类型服务
	@BeanInject
	private TrainClassroomService trainClassroomService; // 培训室服务
	@BeanInject
	private TrainLecturerService trainLecturerService; // 培训讲师服务
	@BeanInject
	private TrainPlanDutyTargetService trainPlanDutyTargetService; // 培训计划职务选择对象服务
	@BeanInject
	private TrainMaterResourceService trainMaterResourceService; // 培训资源服务

	private List<Organization> organizations;
	private LimitedList<TrainPlan> plans;
	private LimitedList<TrainCategory> categories;
	private LimitedList<TrainLecturer> lecturers;
	private LimitedList<TrainClassroom> classrooms;
	private TrainPlan plan;
	private Long planId;
	private String ids;
	private Long dutyId;
	private Duty duty;
	private LimitedList<Duty> duties;
	// 培训职务对象
	private LimitedList<TrainPlanDutyTarget> dutyTargets;

	// -----------------培训计划申请审核模块-----------------
	private List<List<TrainPlanAuditing>> audits;

	private LimitedList<EmployeeinfoWork> works;

	// ------------------培训计划更正模块--------------
	private String amendStartDate; // 更正日期
	private String amendEndDate; // 更正时间
	private String amendReason; // 更正原因
	private String cancelReason; // 取消原因
	private Long workId;

	@Action("/train/plan_main")
	@Override
	public String execute() throws Exception {
		listTitle = "员工培训计划 - 设置";
		listAction = "/train/plan_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId().longValue() != topOrgId.longValue()){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/plan_list")
	public String list() {
		listAction = "/train/plan_list.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, dutyId, 0,
				AuditStatus.wait_audit, createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/plan_list.xhtml");
		return "list";
	}

	// 审核
	@Action("/train/plan_audit_main")
	public String executePlanAudit() throws Exception {
		listTitle = "员工培训计划 - 审核";
		listAction = "/train/plan_list_audit.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/plan_list_audit")
	public String dispatchPlanAuditList() {
		listAction = "/train/plan_list_audit.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, dutyId, 0, null,
				createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/plan_list_audit.xhtml");
		return "plan_list_audit";
	}

	// ////////////////////////////////
	@Action("/train/plan_browse_main")
	public String dispatchPlanBrowse() throws Exception {
		listTitle = "员工培训计划 - 浏览";
		listAction = "/train/plan_list_browse.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/plan_list_browse")
	public String browsePlanList() {
		listAction = "/train/plan_list_browse.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, dutyId, 0,
				AuditStatus.pass_audit, createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/plan_list_browse.xhtml");
		return "plan_list_browse";
	}

	// ///////////////培训计划执行////////////////
	@Action("/train/plan_execute_main")
	public String dispatchPlanExecute() throws Exception {
		listTitle = "培训执行准备";
		listAction = "/train/plan_list_execute.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/train/plan_list_execute")
	public String browsePlanExecuteList() {
		listAction = "/train/plan_list_execute.xhtml";
		plans = trainPlanService.getTrainPlans(organizationId, dutyId, 0, null,
				createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("train/plan_list_execute.xhtml");
		return "plan_list_execute";
	}

	// ///////////////内训评价/////////////////

	/**
	 * 转向培训计划设置处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/plan_manage")
	public String dispatchPlan() throws Exception {
		boolean isFirst = getParameter("p") == null ? false : true;
		if (planId == null || planId <= 0) {
			// 添加
			plan = new TrainPlan();
			plan.setOrganization(organizationService.get(organizationId));
		} else {
			// 修改
			plan = trainPlanService.getTrainPlan(planId);
			if (plan.getOrganization().getTopOrganization() == null) {
				organizationId = plan.getOrganization().getId();
			} else {
				organizationId = plan.getOrganization().getTopOrganization()
						.getId();
			}
		}

		HttpSession session = ServletActionContext.getRequest().getSession();
		Object obj = session.getAttribute("current_plan_form");
		if (obj != null) {
			plan = (TrainPlan) obj;
			session.removeAttribute("current_plan_form");
		}
		// 培训类别
		categories = trainCategoryService.getTrainCategories(organizationId,
				null, null);
		// 培训室
		classrooms = trainClassroomService.getEffectiveTrainClassrooms(
				organizationId, null, null);
		// 培训讲师
		lecturers = trainLecturerService.getEffectiveTrainLecturers(
				organizationId, -1L, null, null);

//		StringBuffer jsonString = new StringBuffer();
		// 读取培训职务对象列表
		// 检测是否已经选择对象返回
		dutyTargets = new LimitedList<TrainPlanDutyTarget>();
		boolean isHasSelectObject = false;
		List<TrainPlanDutyTargetPojo> dutyTargetPojos = new ArrayList<TrainPlanDutyTargetPojo>();
		if (!isFirst) {
			if(session.getAttribute("dutytarget") != null){
				//jsonString.append(session.getAttribute("dutytarget").toString().replace("\"", "'"));
				dutyTargetPojos = (List<TrainPlanDutyTargetPojo>) session.getAttribute("dutytarget");
				if(dutyTargetPojos != null && dutyTargetPojos.size() > 0){
					Iterator<TrainPlanDutyTargetPojo> it = dutyTargetPojos.iterator();
					while(it.hasNext()){
						TrainPlanDutyTargetPojo dutyTargetPojo = it.next();
						TrainPlanDutyTarget dutyTarget = new TrainPlanDutyTarget();
						dutyTarget.setOrganization(organizationService.get(dutyTargetPojo.getOrganizationId()));
						dutyTarget.setDuty(dutyService.get(dutyTargetPojo.getDutyId()));
						dutyTarget.setCredit(dutyTargetPojo.getCredit());
						dutyTargets.add(dutyTarget);
					}
				}
				isHasSelectObject = true;
			}
		}else{
			session.removeAttribute("dutytarget");
		}
		if (!isHasSelectObject) {
			dutyTargets = trainPlanDutyTargetService.getTrainPlanDutyTargets(
					planId, null, null);
			if(dutyTargets != null && dutyTargets.size() > 0){
				Iterator<TrainPlanDutyTarget> it = dutyTargets.iterator();
				while(it.hasNext()){
					TrainPlanDutyTarget dutyTarget = it.next();
					TrainPlanDutyTargetPojo dutyTargetPojo = new TrainPlanDutyTargetPojo();
					dutyTargetPojo.setOrganizationId(dutyTarget.getOrganization().getId());
					dutyTargetPojo.setDutyId(dutyTarget.getDuty().getId());
					dutyTargetPojo.setDutyName(dutyTarget.getDuty().getDutyName());
					dutyTargetPojo.setCredit(dutyTarget.getCredit());
					dutyTargetPojos.add(dutyTargetPojo);
				}
				session.setAttribute("dutytarget", dutyTargetPojos);
			}
		}
		return "plan_manage";
	}

	/**
	 * 选择培训目标
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/target_sel_main")
	public String dispatchPlanTarget() throws Exception {
		listAction = "/train/target_sel_list.xhtml";
		listTitle = "员工培训计划设置 - 培训对象选择";
		/*HttpSession session = ServletActionContext.getRequest().getSession();
		if(session.getAttribute("dutytarget") != null){
			//jsonString.append(session.getAttribute("dutytarget").toString().replace("\"", "'"));
			dutyTargets = (LimitedList<TrainPlanDutyTarget>) session.getAttribute("dutytarget");
		}*/
		return "target_sel_main";
	}

	@Action("/train/target_sel_list")
	public String dispatchTargetList() throws Exception {
		if (duty == null) {
			duty = new Duty();
		}
		Organization o = new Organization();
		o.setId(organizationId);
		duty.setOrganization(o);

		duties = dutyService.search(duty, createLimit(), createSorter());

		/* 分页 */
		this.paging(duties);
		/* 设置翻页action */
		this.setPageUrl("train/target_sel_list.xhtml");

		return "target_sel_list";
	}

	/**
	 * 提交培训计划
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/post_plan")
	public String postPlan() throws Exception {
		//
		if (plan == null) {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		if (plan.getId() == null) {
			// 添加
		} else {
			// 修改
		}
		plan.setLecturerType(0); // 内训
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		plan.setPlanStartDate(formatter.parse(getParameter("planStartDate")));
		plan.setPlanEndDate(formatter.parse(getParameter("planEndDate")));
		plan.setTrainStartDate(formatter.parse(getParameter("planStartDate")));
		plan.setTrainEndDate(formatter.parse(getParameter("planEndDate")));
//		String strDutyTargetParam = getParameter("duty_target") == null ? ""
//				: getParameter("duty_target");
//		if (strDutyTargetParam.startsWith("\"")) {
//			strDutyTargetParam = strDutyTargetParam.substring(1,
//					strDutyTargetParam.length() - 1);
//		}
//		String jsonDutyTargets = strDutyTargetParam.isEmpty() ? "[]" : "["
//				+ strDutyTargetParam + "]";
		HttpSession session = ServletActionContext.getRequest().getSession();
		List<TrainPlanDutyTargetPojo> employeeTargetPojos = new ArrayList<TrainPlanDutyTargetPojo>();
		if(session.getAttribute("dutytarget") != null){
			employeeTargetPojos = (List<TrainPlanDutyTargetPojo>)session.getAttribute("dutytarget");
		}
		if(employeeTargetPojos == null || employeeTargetPojos.size() <= 0){
			return ERROR;
		}
		// 保存数据
		plan.setAuditStatus(AuditStatus.wait_audit);
		try {
			// 保存培训计划内容
			trainPlanService.save(plan);
			// 获取planId
			// 保存培训对象内容
			trainPlanDutyTargetService.deleteTrainPlanDutyTargetByPlanId(plan
					.getId());
//			JSONArray jsonArray = JSONArray.fromObject(jsonDutyTargets);
			if (employeeTargetPojos != null && employeeTargetPojos.size() > 0) {
				for (int i = 0; i < employeeTargetPojos.size(); i++) {
					TrainPlanDutyTarget dutyTarget = new TrainPlanDutyTarget();
					dutyTarget.setTrainPlan(plan);
					dutyTarget.setDuty(dutyService.get(employeeTargetPojos.get(i).getDutyId()));
					dutyTarget.setOrganization(organizationService.get(employeeTargetPojos.get(i).getOrganizationId()));
					dutyTarget.setCredit(employeeTargetPojos.get(i).getCredit());
					trainPlanDutyTargetService.save(dutyTarget);
				}
			}

//			// 清Cookie
//			Cookie[] cookies = ServletActionContext.getRequest().getCookies();
//			if (cookies != null && cookies.length > 0) {
//				for (Cookie mCookie : cookies) {
//					if (mCookie.getName().equals("dutytarget")) {
//						mCookie.setValue("");
//					}
//				}
//			}
			session.removeAttribute("dutytarget");

		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "main";
	}

	/**
	 * 删除培训计划
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action(value = "/train/plan_delete", results = { @Result(name = "success", type = "plainText") })
	public String deletePlan() throws Exception {
		if (StringUtils.isNotBlank(ids)) {
			try {
				// 删除培训对象
				trainPlanDutyTargetService
						.deleteTrainPlanDutyTargetByPlanIds(ids);
				// 删除培训资源
				for (String s : ids.split(",")) {
					List<TrainMaterResource> list = trainMaterResourceService
							.getTrainMaterResourcesByPlanId(Long.parseLong(s),
									null, null);
					for (TrainMaterResource trainMaterResource : list) {
						File f = new File(ServletActionContext.getRequest()
								.getRealPath(
										trainMaterResource.getCoursewareUrl()));
						if (f.exists()) {
							f.delete();
						}
					}
				}
				trainMaterResourceService
						.deleteTrainMaterResourceByPlanIds(ids);

				// 删除培训计划
				trainPlanService.deleteTrainPlan(ids);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(e.getMessage());
				}
				return ERROR;
			}

		}

		return SUCCESS;
	}

	/**
	 * 培训计划申请审核处理
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/plan_audit")
	public String handleTrainPlanAuditing() throws Exception {

		if (planId == null) {
			return ERROR;
		}
		try {

			plan = trainPlanService.getTrainPlan(planId);
			if (plan == null) {
				return ERROR;
			}

			dutyTargets = trainPlanDutyTargetService.getTrainPlanDutyTargets(
					planId, null, null);
			audits = trainPlanService.searchTrainPlanAuditings(plan.getId(),
					null, null);
			if (audits == null || audits.isEmpty()) {
				trainPlanService.saveTrainPlanAuditingProcess(plan, 1);
				audits = trainPlanService.searchTrainPlanAuditings(
						plan.getId(), null, null);
			}
            //检查当前登录用户是否可以审核当前结点
            if(audits != null && !audits.isEmpty()){
                checkCanAudit((Duty)null, audits.get(audits.size()-1));
            }
		} catch (Exception e) {
			e.printStackTrace(); // To change body of catch statement use File |
			// Settings | File Templates.
			throw new RuntimeException(e.getMessage());
		}
		currentAuditType = AuditType.audit_train_plan.toString();
		return "plan_audit";
	}

	/**
	 * 签到
	 * 
	 * @return
	 * @throws Exception
	 */
	@Action("/train/plan_signin")
	public String handleTrainPlanSign() throws Exception {
		if (planId == null || planId < 0) {
			return ERROR;
		}
		try {
			plan = trainPlanService.getTrainPlan(planId);
			works = trainPlanDutyTargetService.getEmploymentsByPlanId(planId,
					null, null);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		return "plan_signin";
	}

	@Action("/train/plan_notify")
	public String handleTrainPlanNotify() throws Exception {
		if (planId == null || planId < 0) {
			return ERROR;
		}
		try {
			plan = trainPlanService.getTrainPlan(planId);
			works = trainPlanDutyTargetService.getEmploymentsByPlanId(planId,
					null, null);
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		}
		return "plan_notify";
	}

	/**
	 * 更正培训内容
	 * 
	 * @return
	 */
	@Action("/train/plan_delay_manage")
	public String handleAmendPlan() {
		if (planId == null || planId < 0) {
			return ERROR;
		}
		try {
			plan = trainPlanService.getTrainPlan(planId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "plan_delay_manage";
	}

	@Action("/train/post_delay_plan")
	public String postDelayPlan() throws Exception {
		//
		if (amendEndDate == null || amendStartDate == null || amendReason == "") {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		plan = trainPlanService.getTrainPlan(planId);
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		plan.setPlanStartDate(formatter.parse(getParameter("amendStartDate")));
		plan.setPlanEndDate(formatter.parse(getParameter("amendEndDate")));
		plan.setAmendStartDate(formatter.parse(getParameter("amendStartDate")));
		plan.setAmendEndDate(formatter.parse(getParameter("amendEndDate")));
		plan.setAmendReason(amendReason);
		// 设置审核状态重新审核
		plan.setAuditStatus(AuditStatus.wait_audit);
		try {
			// 保存培训计划内容
			trainPlanService.save(plan);
			// 重新创建审核流程
			audits = trainPlanService.searchTrainPlanAuditings(plan.getId(),
					null, null);
			if (audits == null || audits.isEmpty()) {
				trainPlanService.saveTrainPlanAuditingProcess(plan, 1);
			} else {
				trainPlanService.saveTrainPlanAuditingProcess(plan,
						audits.get(audits.size() - 1).get(0).getAuditNo() + 1);
			}
		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "plan_browse_main";
	}

	/**
	 * 更正培训内容
	 * 
	 * @return
	 */
	@Action("/train/plan_cancel_manage")
	public String handleCancelPlan() {
		if (planId == null || planId < 0) {
			return ERROR;
		}
		try {
			plan = trainPlanService.getTrainPlan(planId);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "plan_cancel_manage";
	}

	@Action("/train/post_cancel_plan")
	public String postCancelPlan() throws Exception {
		//
		if (cancelReason == null) {
			if (log.isErrorEnabled()) {
				log.error("提交数据为空");
				return ERROR;
			}
		}
		plan = trainPlanService.getTrainPlan(planId);
		plan.setCancel(1);
		plan.setCancelReason(cancelReason);

		plan.setAuditStatus(AuditStatus.wait_audit);
		try {
			// 保存培训计划内容
			trainPlanService.save(plan);
			// 重新创建审核流程
			audits = trainPlanService.searchTrainPlanAuditings(plan.getId(),
					null, null);
			if (audits == null || audits.isEmpty()) {
				trainPlanService.saveTrainPlanAuditingProcess(plan, 1);
			} else {
				trainPlanService.saveTrainPlanAuditingProcess(plan,
						audits.get(audits.size() - 1).get(0).getAuditNo() + 1);
			}
		} catch (Exception e) {
			log.equals(e.getMessage());
			return ERROR;
		}
		return "plan_browse_main";
	}

	// 个人调用模块
	@Action("/public/plan_person_main")
	public String dispatchPersonPlan() throws Exception {
		listTitle = "员工培训计划 - 设置";
		listAction = "/public/plan_person_list.xhtml";
		organizations = organizationService
				.findChildren(null, true, null, null);
		if (organizations != null && organizations.size() > 0) {
			if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
			organizationId = organizations.get(0).getId();
		}
		showChildOrg = true;
		return "main";
	}

	@Action("/public/plan_person_list")
	public String dispatchPersonPlanList() {
		listAction = "/public/plan_person_list.xhtml";
		plans = trainPlanService.getTrainPlansByUserId(organizationId, dutyId,
				getCurrentUserId(), createLimit(), createSorter());
		paging(plans);
		/* 设置翻页action */
		this.setPageUrl("public/plan_person_list.xhtml");
		return "plan_person_list";
	}
	
	/** 员工培训列表 */
	@Action("/public/staff_plan_list")
	public String dispatchStaffPlanList() {
		listAction = "/public/staff_plan_list.xhtml";
		try {
			plans = trainPlanService.getTrainPlansByWorkId(workId, createLimit(), createSorter());
			paging(plans);
			/* 设置翻页action */
			this.setPageUrl("public/staff_plan_list.xhtml");
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "staff_plan_list";
	}

	public void setOrganizations(List<Organization> organizations) {
		this.organizations = organizations;
	}

	public List<Organization> getOrganizations() {
		return organizations;
	}

	public LimitedList<TrainPlan> getPlans() {
		return plans;
	}

	public void setPlans(LimitedList<TrainPlan> plans) {
		this.plans = plans;
	}

	public LimitedList<TrainCategory> getCategories() {
		return categories;
	}

	public void setCategories(LimitedList<TrainCategory> categories) {
		this.categories = categories;
	}

	public LimitedList<TrainLecturer> getLecturers() {
		return lecturers;
	}

	public void setLecturers(LimitedList<TrainLecturer> lecturers) {
		this.lecturers = lecturers;
	}

	public LimitedList<TrainClassroom> getClassrooms() {
		return classrooms;
	}

	public void setClassrooms(LimitedList<TrainClassroom> classrooms) {
		this.classrooms = classrooms;
	}

	public TrainPlan getPlan() {
		return plan;
	}

	public void setPlan(TrainPlan plan) {
		this.plan = plan;
	}

	public Long getPlanId() {
		return planId;
	}

	public void setPlanId(Long planId) {
		this.planId = planId;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public Long getDutyId() {
		return dutyId;
	}

	public void setDutyId(Long dutyId) {
		this.dutyId = dutyId;
	}

	public Duty getDuty() {
		return duty;
	}

	public void setDuty(Duty duty) {
		this.duty = duty;
	}

	public LimitedList<Duty> getDuties() {
		return duties;
	}

	public void setDuties(LimitedList<Duty> duties) {
		this.duties = duties;
	}

	public LimitedList<TrainPlanDutyTarget> getDutyTargets() {
		return dutyTargets;
	}

	public void setDutyTargets(LimitedList<TrainPlanDutyTarget> dutyTargets) {
		this.dutyTargets = dutyTargets;
	}

	public List<List<TrainPlanAuditing>> getAudits() {
		return audits;
	}

	public void setAudits(List<List<TrainPlanAuditing>> audits) {
		this.audits = audits;
	}

	public LimitedList<EmployeeinfoWork> getWorks() {
		return works;
	}

	public void setWorks(LimitedList<EmployeeinfoWork> works) {
		this.works = works;
	}

	public String getAmendStartDate() {
		return amendStartDate;
	}

	public void setAmendStartDate(String amendStartDate) {
		this.amendStartDate = amendStartDate;
	}

	public String getAmendEndDate() {
		return amendEndDate;
	}

	public void setAmendEndDate(String amendEndDate) {
		this.amendEndDate = amendEndDate;
	}

	public String getAmendReason() {
		return amendReason;
	}

	public void setAmendReason(String amendReason) {
		this.amendReason = amendReason;
	}

	public String getCancelReason() {
		return cancelReason;
	}

	public void setCancelReason(String cancelReason) {
		this.cancelReason = cancelReason;
	}
	
	public Long getWorkId() {
		return workId;
	}
	
	public void setWorkId(Long workId) {
		this.workId = workId;
	}
}
